#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gnomeagenda.pluginsystem import *

class CalendarProviderBase(Plugin):
	capabilities = []
	"""This is the base class for all calendar Providers"""
	def __init__(self):
		Plugin.__init__(self)
		self.errors = []
		self.timezoneadjust = 0
		self.disabled = False
		self.eventStorage = []
		self.calendarStorage = {}
		self.update_ok = False
	def initPlugin(self,options):
		
		self.setOptions(options)
		pass
	def clearErrors(self):
		self.errors = []
	def notifyError(self,errorMessage):
		"""Notify the user on Errors that occured during the update"""
		print errorMessage
		self.errors.append(errorMessage)
	def getErrors(self):
		"""return the errors"""
		return self.errors
	def deleteEvents(self):
		self.eventStorage = []
		self.calendarStorage = {}		
	def addEvent(self, id, url, start, end, title, description = "", place = "", status = "CONFIRMED"):
		"""add a new event to the internal even storage."""
		title = self.ensureString(title)
		description = self.ensureString(description)
		place = self.ensureString(place)

		title = self.escape(self.unescape(title))
		description = self.escape(self.unescape(description))
		place = self.escape(self.unescape(place))
		#print (id, url, start, end, title, description, place, status)
		self.eventStorage.append({'id':id,'cal_id':url,'start':start,'end':end,'title':title,'description': description,'place':place,'status':status})
	def addCalendar(self,id,name,color,updated_ext = None,hidden = False):
		name = self.escape(self.unescape(name))		
		if(not self.calendarStorage.has_key(id)):
			self.calendarStorage[id] = [id,name,color,updated_ext,hidden]
			#print "added Calendar %s" % name
			#print (id,name,color,updated_ext,hidden)
	def ensureString(self,val):
		if (val == None):
			return ""
		else:
			return val
	def unescape(self,s):
		s = s.replace("&lt;", "<")
		s = s.replace("&gt;", ">")
		# this has to be last:
		s = s.replace("&amp;", "&")
		return s
	def escape(self,s):
		s = s.replace("<","&lt;")
		s = s.replace(">","&gt;")
		# this has to be last:
		s = s.replace("&","&amp;")
		return s

	def list_calendars(self):
		""" return a list with all calendars available with this provider"""
		return []
